package com.poly.flowable.controller;

import com.poly.common.core.domain.R;
import com.poly.common.core.utils.poi.ExcelUtil;
import com.poly.common.core.web.controller.BaseController;
import com.poly.common.core.web.page.TableDataInfo;
import com.poly.common.security.annotation.RequiresPermissions;
import com.poly.flowable.domain.SysForm;
import com.poly.flowable.service.ISysFormService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 流程表单Controller
 *
 * @author Yang huijing
 * @date 2021-04-03
 */
@Api(tags = "表单管理")
@RestController
@RequestMapping("/form")
public class SysFormController extends BaseController {
    @Autowired
    private ISysFormService sysFormService;

    /**
     * 查询流程表单列表
     */
    @RequiresPermissions("flowable:form:list")
    @GetMapping("/list")
    public TableDataInfo list(SysForm sysForm) {
        startPage();
        List<SysForm> list = sysFormService.selectSysFormList(sysForm);
        return getDataTable(list);
    }

    @GetMapping("/formList")
    public R<List<SysForm>> formList(SysForm sysForm) {
        List<SysForm> list = sysFormService.selectSysFormList(sysForm);
        return R.ok(list);
    }
    /**
     * 导出流程表单列表
     */
    @RequiresPermissions("flowable:form:export")
    @GetMapping("/export")
    public void export(HttpServletResponse response, SysForm sysForm) {
        List<SysForm> list = sysFormService.selectSysFormList(sysForm);
        ExcelUtil<SysForm> util = new ExcelUtil<SysForm>(SysForm.class);
        util.exportExcel(response,list, "form");
    }

    /**
     * 获取流程表单详细信息
     */
    @RequiresPermissions("flowable:form:query")
    @GetMapping(value = "/{formId}")
    public R<SysForm> getInfo(@PathVariable("formId") Long formId) {
        return R.ok(sysFormService.selectSysFormById(formId));
    }

    /**
     * 新增流程表单
     */
    @RequiresPermissions("flowable:form:add")
    @PostMapping
    public R add(@RequestBody SysForm sysForm) {
        return toResult(sysFormService.insertForm(sysForm));
    }

    /**
     * 修改流程表单
     */
    @RequiresPermissions("flowable:form:edit")
    @PutMapping
    public R edit(@RequestBody SysForm sysForm) {
        return toResult(sysFormService.updateForm(sysForm));
    }

    /**
     * 删除流程表单
     */
    @RequiresPermissions("flowable:form:remove")
    @DeleteMapping("/{formIds}")
    public R remove(@PathVariable Long[] formIds) {
        return toResult(sysFormService.deleteSysFormByIds(formIds));
    }

}
